package com.shlh.saas.controller;

import com.shlh.saas.common.Result;
import com.shlh.saas.common.UserContext;
import com.shlh.saas.entity.WorkbenchStats;
import com.shlh.saas.service.WorkbenchStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * 工作台控制器
 */
@RestController
@RequestMapping("/dashboard")
public class WorkbenchController {

    @Autowired
    private WorkbenchStatsService workbenchStatsService;
    
    /**
     * 获取工作台统计数据
     * @return Result
     */
    @GetMapping("/stats")
    public Result getWorkbenchStats() {
        Long userId = UserContext.getUserId();
        WorkbenchStats stats = workbenchStatsService.getByUserId(userId);
        
        Map<String, Object> result = new HashMap<>();
        
        // 海关数据
        Map<String, Object> customs = new HashMap<>();
        customs.put("exportCount", stats.getCustomsExportCount());
        customs.put("searchCount", stats.getCustomsSearchCount());
        result.put("customs", customs);
        
        // 全球企业库
        Map<String, Object> globalEnterprise = new HashMap<>();
        globalEnterprise.put("exportCount", stats.getEnterpriseExportCount());
        globalEnterprise.put("searchCount", stats.getEnterpriseSearchCount());
        result.put("globalEnterprise", globalEnterprise);
        
        // 广交会采购商
        Map<String, Object> cantonPurchasers = new HashMap<>();
        cantonPurchasers.put("exportCount", stats.getCantonBuyerExportCount());
        cantonPurchasers.put("searchCount", stats.getCantonBuyerSearchCount());
        result.put("cantonPurchasers", cantonPurchasers);
        
        // 广交会参展商
        Map<String, Object> cantonExhibitors = new HashMap<>();
        cantonExhibitors.put("exportCount", stats.getCantonExhibitorExportCount());
        cantonExhibitors.put("searchCount", stats.getCantonExhibitorSearchCount());
        result.put("cantonExhibitors", cantonExhibitors);
        
        // LinkedIn获客
        Map<String, Object> linkedin = new HashMap<>();
        linkedin.put("exportCount", stats.getLinkedinExportCount());
        linkedin.put("searchCount", stats.getLinkedinSearchCount());
        result.put("linkedin", linkedin);
        
        // Facebook获客
        Map<String, Object> facebook = new HashMap<>();
        facebook.put("exportCount", stats.getFacebookExportCount());
        facebook.put("searchCount", stats.getFacebookSearchCount());
        result.put("facebook", facebook);
        
        // Twitter获客
        Map<String, Object> twitter = new HashMap<>();
        twitter.put("exportCount", stats.getTwitterExportCount());
        twitter.put("searchCount", stats.getTwitterSearchCount());
        result.put("twitter", twitter);
        
        return Result.success(result);
    }
    
    /**
     * 获取工具使用统计数据
     * @return Result
     */
    @GetMapping("/search-stats")
    public Result getSearchCountStats() {
        Long userId = UserContext.getUserId();
        WorkbenchStats stats = workbenchStatsService.getByUserId(userId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("intelligentMailSearchCount", stats.getIntelligentSearchCount());
        result.put("emailReverseSearchCount", stats.getEmailReverseCount());
        result.put("domainReverseSearchCount", stats.getDomainLookupCount());
        result.put("socialMediaReverseSearchCount", stats.getSocialMediaLookupCount());
        
        return Result.success(result);
    }
} 